package com.ruoyi.cscec.controller;

import java.io.*;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cscec.util.pdfUtil.PdfReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.cscec.domain.DataTotal;
import com.ruoyi.cscec.service.IDataTotalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 数据汇总表Controller
 *
 * @author ruoyi
 * @date 2021-09-27
 */
@RestController
@RequestMapping("/total")
public class DataTotalController extends BaseController
{
    @Autowired
    private IDataTotalService dataTotalService;

    /**
     * 查询数据汇总表列表
     */
    @PreAuthorize(hasPermi = "cscec:total:list")
    @GetMapping("/list")
    public TableDataInfo list(DataTotal dataTotal)
    {
        startPage();
        List<DataTotal> list = dataTotalService.selectDataTotalList(dataTotal);
        return getDataTable(list);
    }

    /**
     * 导出数据汇总表列表
     */
    @PreAuthorize(hasPermi = "cscec:total:export")
    @Log(title = "数据汇总表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataTotal dataTotal) throws IOException
    {
        List<DataTotal> list = dataTotalService.selectDataTotalList(dataTotal);
        ExcelUtil<DataTotal> util = new ExcelUtil<DataTotal>(DataTotal.class);
        util.exportExcel(response, list, "数据汇总表数据");
    }

    /**
     * 获取数据汇总表详细信息
     */
    @PreAuthorize(hasPermi = "cscec:total:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dataTotalService.selectDataTotalById(id));
    }

    /**
     * 新增数据汇总表
     */
    @PreAuthorize(hasPermi = "cscec:total:add")
    @Log(title = "数据汇总表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataTotal dataTotal)
    {
        return toAjax(dataTotalService.insertDataTotal(dataTotal));
    }

    /**
     * 修改数据汇总表
     */
    @PreAuthorize(hasPermi = "cscec:total:edit")
    @Log(title = "数据汇总表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataTotal dataTotal)
    {
        return toAjax(dataTotalService.updateDataTotal(dataTotal));
    }

    /**
     * 删除数据汇总表
     */
    @PreAuthorize(hasPermi = "cscec:total:remove")
    @Log(title = "数据汇总表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dataTotalService.deleteDataTotalByIds(ids));
    }

    /**
     * 导出检查PDF
     */
    @PreAuthorize(hasPermi = "inspect:inspectrecord:export")
    @Log(title = "检查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/pdf/export")
    public void wordExport(HttpServletResponse response,DataTotal dataTotal)
    {
        File file=null;
        try {
            List<DataTotal> dataTotals = dataTotalService.selectDataTotalList(dataTotal);
            file = PdfReport.GeneragePDF(dataTotals, UUID.randomUUID().toString());
            String path = file.getPath();
            response.setCharacterEncoding("UTF-8");
            //设置显示文件的类型
            response.setContentType("application/pdf");
            InputStream in = new FileInputStream(file);
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            //创建存放文件内容的数组
            byte[] buff =new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读取完时,继续读取,循环
            while((n=in.read(buff))!=-1){
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff,0,n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            File file1 = new File(file.toURI());
            file1.delete();
        }
    }
}
